package com.lsz.flight_system.controller;

import com.lsz.flight_system.entity.UserAssess;
import com.lsz.flight_system.entity.UserMessage;
import com.lsz.flight_system.service.UserAssessService;
import com.lsz.flight_system.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/userAssess")
public class UserAssessController {

    @Autowired
    UserAssessService userAssessService;

    @ResponseBody
    @GetMapping("/user/userAssesses")
    public Result userViewUserAssess(){
        List<UserAssess> userAssessList = userAssessService.userFindAllUserAssess();
        return Result.ok(userAssessList);
    }

    @ResponseBody
    @GetMapping("/admin/userAssesses")
    public Result adminViewAssess(@RequestParam(value = "username",required = false) String username,
                                  @RequestParam(value = "score",required = false) Integer score){
        Map<String,Object> queryMap = new HashMap<>();

        queryMap.put("username",username);
        queryMap.put("score", score);
//        queryMap.put("assessTime",assessTime);

        List<UserAssess> assessList = userAssessService.adminFindAllUserAssess(queryMap);

        return Result.ok(assessList);
    }

    @PostMapping("/userDelUserAssess/{id}")
    public Result userDelUserAssessById(@PathVariable Long id, HttpSession session){
        UserMessage userMessage = (UserMessage) session.getAttribute("USER_MESSAGE");
        if (userMessage == null){
            return Result.fail("请登录后进行操作");
        }
        UserAssess userAssess = userAssessService.findUserAssessById(id);
        if (!userMessage.getUsername().equals(userAssess.getUsername())){
            return Result.fail("不能删除别人的用户评价");
        }
        if (userAssessService.delUserAssessById(id)){
            return Result.ok();
        }
        return Result.fail("删除异常");
    }

    @PostMapping("/adminDelUserAssess/{id}")
    public Result adminDelUserAssessById(@PathVariable Long id){
        if (userAssessService.delUserAssessById(id)){
            return Result.ok();
        }
        return Result.fail("删除异常");
    }

    @GetMapping("/userAddUserAssess")
    public Result userAddUserAssess(@RequestParam(value = "assessText",required = false) String assessText,
                                    @RequestParam(value = "score",required = false) Integer score,
                                    @RequestParam(value = "unnamed",required = false) String unnamed,
                                    HttpSession session){
        UserMessage userMessage = (UserMessage) session.getAttribute("USER_MESSAGE");
        if (userMessage == null){
            return Result.fail("请登录后进行评价");
        }
        String text = assessText.replace(" ","").replace("\n","").replace("\r","");

        if (score.toString().equals("")||text.equals("")||unnamed.equals("")){
            return Result.fail("有必填项未填写");
        }

        UserAssess userAssess = new UserAssess();

        userAssess.setUsername(userMessage.getUsername());
        userAssess.setAssessText(assessText);
        userAssess.setScore(score);
        userAssess.setUnnamed(unnamed);

        if (userAssessService.userAddUserAssess(userAssess)){
            return Result.ok();
        }
        return Result.fail("添加异常");
    }
}
